/*
 * RemoveElementAction.java
 *
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.actions.list;

import java.util.Arrays;
import javax.swing.JOptionPane;
import org.tiberiumlabs.lailaps.actions.AbstractElementAction;
import org.tiberiumlabs.lailaps.actions.events.DataEvent;
import org.tiberiumlabs.lailaps.list.DownloadItem;
import org.tiberiumlabs.lailaps.list.StatusConstants;
import org.tiberiumlabs.lailaps.resources.ImageLoader;
import org.tiberiumlabs.lailaps.settings.Settings;
import org.tiberiumlabs.lailaps.toolkit.StateManager;

/**
 *
 * @author <a href="paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class RemoveElementAction extends AbstractElementAction implements StatusConstants {

    /** Creates a new instance of RemoveElementAction */
    public RemoveElementAction() {
        putValue(NAME, bundle.getString("list.remove.name"));
        putValue(SHORT_DESCRIPTION, bundle.getString("list.remove.short"));
        putValue(LONG_DESCRIPTION, bundle.getString("list.remove.long"));
        putValue(LARGE_ICON_KEY, ImageLoader.getRemoveIcon());
    }

    @Override
    protected void performElementAction(int[] rows) {

        // check if we even need to ask:
        if (Settings.getApplicationSettings().isAskBeforeRemove()) {
            // if we need to ask and user dont choose OK - return out of here.
            // we dont need to check for visibility of main window here, because we cant even invoke this action without main window visible.
            if (JOptionPane.showConfirmDialog(mediator.getGuiBuilder().getMainWindow(), bundle.getString("list.remove.confirmation.message"), bundle.getString("list.remove.confirmation.title"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
                return;
            }
        }

        // sort to stop threads backwards
        Arrays.sort(rows);

        int stopped = 0;
        int completed = 0;
        int working = 0;
        for (int i = rows.length - 1; i >= 0; i--) {
            DownloadItem item = model.getItem(rows[i]);
            switch (item.getStatus()) {
                case COMPLETED:
                    {
                        completed++;
                        continue;
                    }
                case ERROR:
                case STOPPED:
                    {
                        stopped++;
                        continue;
                    }
                default:
                    {
                        // if other status - need to stop it
                        threadsMap.remove(item);
                        working++;
                    }
            }
        }

        // remove from model
        model.removeItemsList(rows);

        // notify other threads about changes
        int length = rows.length;
        threadsMap.notify(new DataEvent(DataEvent.REMOVED, rows[0], rows[length - 1], length));

        // count statistics
        StateManager stateManager = mediator.getStateManager();
        stateManager.remove(StateManager.COMPLETED, completed);
        stateManager.remove(StateManager.STOPPED, stopped);
        stateManager.remove(StateManager.WORKING, working);
        
    }
}